package com.bocai.dahuatech.ui.activity;

import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.bocai.dahuatech.Bean.BaseBean;
import com.bocai.dahuatech.Bean.CityListBean;
import com.bocai.dahuatech.R;
import com.bocai.dahuatech.base.BaseActivity;
import com.bocai.dahuatech.event.MainEvent;
import com.bocai.dahuatech.ui.adapter.CityListAdapter;
import com.bocai.dahuatech.utils.AESUtils;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 *   省选择
 */
public class ProviceListActivity extends BaseActivity implements AdapterView.OnItemClickListener
{

    @Bind(R.id.lv_city)
    ListView listView;
    CityListBean bean;
    int proviceId;
    String provice;
    @Override
    protected int contentView() {
        return R.layout.activity_city_list;
    }

    @Override
    protected void initView()
    {
        ButterKnife.bind(this);
        initToolbar(R.string.citychoose);
        int what=getIntent().getIntExtra("add",1);
        if(what==1)
        {

            getTestCitys(1, 0);
        }else if (what==100)
        {
            proviceId=getIntent().getIntExtra("proviceId", 0);
            provice=getIntent().getStringExtra("provice");
            bean=new CityListBean();
            List<CityListBean.ContentEntity> content=new ArrayList<>();
            bean.setContent(content);
            CityListBean.ContentEntity contentEntity=new CityListBean.ContentEntity();
            contentEntity.setFID(proviceId);
            contentEntity.setFItemFullName(provice);
            contentEntity.setFItemName(provice);
            contentEntity.setFParentID(0);
            contentEntity.setFLevel(1);
            bean.getContent().add(contentEntity);
            listView.setAdapter(new CityListAdapter(ProviceListActivity.this, bean));

        }
        initEvent();
    }

    @Override
    protected void initEvent()
    {
        super.initEvent();
        listView.setOnItemClickListener(this);
    }

    private void getTestCitys(int level, int parentId)
    {
        getTestApi().getPCA(level, parentId)
                .compose(this.<BaseBean>bindToLifecycle())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }

    Observer<BaseBean> observer = new Observer<BaseBean>()
    {
        @Override
        public void onCompleted()
        {

        }

        @Override
        public void onError(Throwable e)
        {
            hideLoading();
            showToast(R.string.neterror);
            Log.e("error", e.toString());
        }

        @Override
        public void onNext(BaseBean baseBean)
        {
            if (baseBean.getReturnNo().equals("1"))
            {
                String result = AESUtils.decode(baseBean.getContent());
                result = start + result + end;
                Log.e("result", result);
                bean=new Gson().fromJson(result,CityListBean.class);
                listView.setAdapter(new CityListAdapter(ProviceListActivity.this, bean));
            }
        }

    };

    @Override
    public void onEventMainThread(MainEvent event)
    {
        super.onEventMainThread(event);
        if(event.msg.equals("citychoseover"))
        {
            finish();
        }

    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id)
    {
        Intent intent=new Intent(this,CityListActivity.class);
        intent.putExtra("parentId",bean.getContent().get(position).getFID());
        intent.putExtra("province",bean.getContent().get(position).getFItemName());
        startActivity(intent);

    }
}
